package cn.jeefast.rest.controller;

import cn.jeefast.common.entity.*;
import cn.jeefast.common.utils.R;
import cn.jeefast.rest.service.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 患者控制器
 * @author theodo
 * @email 36780272@qq.com
 * @date 2017-10-26 17:27
 */
@RestController
@RequestMapping("/api")
public class MbPatientController {

    @Autowired
    private MbPatientService mbPatientService;
    @Autowired
    private MbResearchService mbResearchService;
    @Autowired
    private MbTreatmentPlanService mbTreatmentPlanService;
    @Autowired
    private MbDialysisWayService mbDialysisWayService;
    @Autowired
    private MbResearchDataService mbResearchDataService;
    @Autowired
    private MbResearchStepService mbResearchStepService;
    @Autowired
    private MbMessageService mbMessageService;
    /**
     * 添加患者
     * @param patient
     * @return
     */
    @RequestMapping("/patient/add")
    public R addPatient(MbPatient patient, HttpServletRequest request){
        if(patient.getId()!=null){
            mbPatientService.updateById(patient);
            return  R.ok("修改成功").put("data",null);
        }
        Long userId = (Long)request.getAttribute("userId");
        patient.setDocId(userId);
        return mbPatientService.addPatient(patient);
    }

    /**
     * 患者列表
     * @param docId 医生ID
     * @param page 分页
     * @param limit 分页
     * @param keyword 搜索关键字
     * @param resId 研究组ID
     * @param stepId 治疗阶段（已完成）
     * @param dig 诊断
     * @return
     */
    @RequestMapping("/patient/list")
    public R patientList(Long docId, Integer page, Integer limit,String keyword,Long[] resId,Long[] stepId,String[] dig){
        if(keyword!=null&&keyword.length()>=1){
            keyword = keyword.replace("\"","");
        }
        if(resId!=null && resId.length==0){
            resId = null;
        }
        if(stepId!=null && stepId.length==0){
            stepId=null;
        }
        if(dig!=null && dig.length==0){
            dig=null;
        }
        page = (page-1)*limit;
        return mbPatientService.patientList(docId,page,limit,keyword,resId,stepId,dig);
    }

    /**
     * 患者详细信息
     * @param pid
     * @return
     */
    @RequestMapping("/patient/data")
    public R patientData(Long pid){
        return mbPatientService.patientData(pid);
    }

    /**
     * 患者详细信息
     * @param patId
     * @return
     */
    @RequestMapping("/patient/detail")
    public R patientDetail(Long patId){
        Map map = new HashMap();
        //查询患者基础信息
        MbPatient mbPatient = mbPatientService.selectById(patId);
        map.put("patient",mbPatient);
        MbResearch mbResearch = mbResearchService.selectById(mbPatient.getResId());
        //添加研究组信息
        map.put("research",mbResearch);
        //添加治疗方案信息
        MbTreatmentPlan mbTreatmentPlan = mbTreatmentPlanService.selectById(mbPatient.getTpId());
        map.put("tp",mbTreatmentPlan);
        //添加透析方式
        MbDialysisWay mbDialysisWay = mbDialysisWayService.selectById(mbPatient.getDwId());
        map.put("dw",mbDialysisWay);
        //根据患者ID查询研究阶段
        EntityWrapper ew = new EntityWrapper();
        ew.eq("patient_id",mbPatient.getId());
        List<MbResearchData> list = mbResearchDataService.selectList(ew);
        List<Map> list1 = new ArrayList<>();
        for (MbResearchData l:list) {
            //根据阶段ID查询阶段名称
            MbResearchStep mbResearchStep = mbResearchStepService.selectById(l.getResStepId());
            Map map0 = new HashMap();
            map0.put("title",mbResearchStep.getStepName());
            map0.put("state",l.getState());
            map0.put("upTime",l.getUpdateDate());
            map0.put("img",l.getDataImg1());
            map0.put("dataId",l.getDataId());
            map0.put("tempId",mbResearchStep.getTempId());
            list1.add(map0);
        }
        map.put("step",list1);
        return R.ok("").put("data",map);
    }

    @RequestMapping("/exportList")
    public R exportList(Long[] ids){
        return mbPatientService.exportList(ids);
    }

//    /**
//     * 搜索条件
//     * @return
//     */
//    @RequestMapping("/researchCondition")
//    public R researchCondition(Long resId){
//        Map map = new HashMap();
//        //查询组别
//        EntityWrapper ew1 = new EntityWrapper();
//        ew1.eq("proj_id",11);
//        List list = mbResearchService.selectList(ew1);
//        map.put("res",list);
//        //查询所有研究阶段
//        EntityWrapper ew2 = new EntityWrapper();
//        if(resId!=null){
//            ew2.eq("res_id",resId);
//        }else{
//            ew2.eq("res_id",1);
//        }
//        List list1 = mbResearchStepService.selectList(ew2);
//        map.put("step",list1);
//        //查询所有的诊断结果（前10）
//        List<Map> maps = mbPatientService.diagnosisList();
//        map.put("dia",maps);
//        return R.ok("").put("data",map);
//    }

    /**
     * 搜索条件
     * @return
     */
    @RequestMapping("/researchCondition")
    public R researchCondition(Long resId){
        Map map = new HashMap();
        //查询组别
        EntityWrapper ew1 = new EntityWrapper();
        ew1.eq("proj_id",11);
        List<MbResearch> list = mbResearchService.selectList(ew1);
        map.put("res",list);
        //查询所有研究阶段
        EntityWrapper ew2 = new EntityWrapper();
        if(resId!=null){
            ew2.eq("res_id",resId);
        }else{
            StringBuffer sb=new StringBuffer();
            for(int i=0;i<list.size();i++) {
                sb.append(list.get(i).getResId()).append(",");
            }
            if(StringUtils.isNotBlank(sb.toString())) {
                sb.deleteCharAt(sb.length() - 1);
            }
            ew2.in("res_id",sb.toString());
            ew2.groupBy("lable");
        }
        List list1 = mbResearchStepService.selectList(ew2);
        map.put("step",list1);
        //查询所有的诊断结果（前10）
        List<Map> maps = mbPatientService.diagnosisList();
        map.put("dia",maps);
        return R.ok("").put("data",map);
    }

    @RequestMapping("/0115")
    public R test0115(){
        List<Long> longs = mbPatientService.selectResIdByPatId(new Long[]{11211021L, 11402088L, 11425986L});
        System.out.println(longs.toString());
        return R.ok();
    }

    @RequestMapping("/patient/delete")
    public R delete(Long id){
        mbPatientService.deleteById(id);
        //删除关联的研究阶段数据
        EntityWrapper ew = new EntityWrapper();
        ew.eq("patient_id",id);
        List<MbResearchData> list = mbResearchDataService.selectList(ew);
        for (MbResearchData m:list) {
            mbResearchDataService.deleteById(m.getDataId());
        }
        //删除关联的消息
        EntityWrapper ew1 = new EntityWrapper();
        ew1.eq("to_id",id);
        List<MbMessage> list1 = mbMessageService.selectList(ew1);
        for (MbMessage ms:list1) {
            mbMessageService.deleteById(ms.getId());
        }
        return R.ok("").put("data",null);
    }
}
